/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package db.genomemap;

import db.gui.RDBConnection;

import genomemap.data.Clone;
import genomemap.data.DNA_RegionUtil;
import genomemap.data.PDataSource;
import genomemap.data.Probe;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedHashSet;

/**
 * @author Susanta Tewari
 * @version 1.0
 * @created 09-Nov-2007 10:23:01 AM
 */
public class P_DBDataSource extends PDataSource {

    /**
     * Constructs ...
     */
    public P_DBDataSource() {}

    /**
     * Constructs ...
     *
     * @param ch_id description
     * @param order description
     */
    public P_DBDataSource(int ch_id, int[] order) {

        setChId(ch_id);
        setOrder(order);
        setProbeCount(order.length);
    }

    @Override
    public void loadData() {

        int[][] hybridDataTemp = null;

        try {

            RDBConnection dbConnection = new RDBConnection();
            Statement stmt             = dbConnection.getStatement();

            if (stmt == null) {
                return;
            }

            ResultSet rs      = null;
            String query      = null;
            String probeNames = getProbenames4Query();

            query = "SELECT " + probeNames + " FROM " + getChData().getHybridTable() + ";";
            rs    = stmt.executeQuery(query);

            rs.last();

            int rowCount = rs.getRow();
            int colCount = (rs.getMetaData()).getColumnCount();

            rs.beforeFirst();

            hybridDataTemp = new int[rowCount][getProbeCount()];

            int rowIndex = 0;

            while (rs.next()) {

                for (int colIndex = 1; colIndex <= colCount; colIndex++) {

                    hybridDataTemp[rowIndex][colIndex - 1] =
                        Integer.parseInt(rs.getString(colIndex));
                }

                rowIndex++;
            }

            dbConnection.close();

        } catch (NumberFormatException | SQLException ex) {
            ex.printStackTrace();
        }

        setHybridData(hybridDataTemp);
    }

    @Override
    public LinkedHashSet<Probe> getFullProbeSet() {

        RDBConnection dbConnection = new RDBConnection();
        Statement stmt             = dbConnection.getStatement();

        if (stmt == null) {
            return null;
        }

        ResultSet rs                  = null;
        String query                  = null;
        LinkedHashSet<Probe> probeSet = new LinkedHashSet<>();

        query = " SELECT p.probe_name,p.start,p.end,p.probe_order FROM probe p "
                + " WHERE p.ch_id = " + getChId() + " ;";

        try {

            rs = stmt.executeQuery(query);

            while (rs.next()) {

                Probe probe = new Probe();

                probe.setLabel(rs.getString("probe_name"));
                probe.setStartPos(rs.getInt("start"));
                probe.setEndPos(rs.getInt("end"));
                probe.setOrder(rs.getInt("probe_order"));
                probeSet.add(probe);
            }

            dbConnection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return probeSet;
    }

    @Override
    public LinkedHashSet<Clone> getCloneSet() {

        RDBConnection dbConnection = new RDBConnection();
        Statement stmt             = dbConnection.getStatement();

        if (stmt == null) {
            return null;
        }

        ResultSet rs                  = null;
        String query                  = null;
        LinkedHashSet<Clone> cloneSet = new LinkedHashSet<>();

        try {

            query = " SELECT c.clone_name,c.start,c.end,c.clone_order FROM clone c "
                    + " WHERE c.ch_id = " + getChId() + " ;";
            rs = stmt.executeQuery(query);

            while (rs.next()) {

                Clone clone = new Clone();

                clone.setLabel(rs.getString("clone_name"));
                clone.setStartPos(rs.getInt("start"));
                clone.setEndPos(rs.getInt("end"));
                clone.setOrder(rs.getInt("clone_order"));
                cloneSet.add(clone);
            }

            rs.next();
            dbConnection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return cloneSet;
    }

    /**
     * Method description
     *
     * @return description
     */
    private String getProbenames4Query() {

        String probeNames4Query           = "";
        LinkedHashSet<String> probeLabels = DNA_RegionUtil.getProbeLabels(getChId(), getOrder());
        Iterator itr                      = probeLabels.iterator();

        for (int index = 0; index < probeLabels.size() - 1; index++) {

            probeNames4Query += itr.next() + ",";
        }

        probeNames4Query += (String) itr.next();

        return probeNames4Query;
    }
}
